package com.jp.refreshlocalcacheclient.net.netty.message;

import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2020/5/14 17:15
 */
public abstract class RequestMessage implements Runnable,Cloneable {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 消息ID
     */
    protected int msgId;

    /**
     * 缓存key
     */
    protected String content;

    /**
     * 会话数据
     */
    protected ChannelHandlerContext channelHandlerContext;


    @Override
    public void run() {
        try {
            process(channelHandlerContext);
        }catch (Exception e){
            logger.info("message id : {} , content : {},  处理异常 : {}",msgId, content, ExceptionUtils.getStackTrace(e));
        }
    }

    /**
     * 具体处理的方法，在各个消息处理类型中重写
     * @param ctx
     */
    protected abstract void process (ChannelHandlerContext ctx);


    @Override
    public RequestMessage clone() throws CloneNotSupportedException {
        return (RequestMessage)super.clone();
    }

    public Logger getLogger() {
        return logger;
    }

    public int getMsgId() {
        return msgId;
    }

    public void setMsgId(int msgId) {
        this.msgId = msgId;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public ChannelHandlerContext getChannelHandlerContext() {
        return channelHandlerContext;
    }

    public void setChannelHandlerContext(ChannelHandlerContext channelHandlerContext) {
        this.channelHandlerContext = channelHandlerContext;
    }
}
